﻿
Public Class DataSorterTypeCollection(Of T)
    Inherits CollectionBase

    Protected Friend iSortOrder As DBTOSortOrder = DBTOSortOrder.Ascending
    Protected Friend iTableName As String


    Public Sub New(ByVal TableName As String)
        Me.iTableName = TableName
    End Sub
    Public Property SortOrder() As DBTOSortOrder
        Get
            Return Me.iSortOrder
        End Get
        Set(ByVal value As DBTOSortOrder)
            Me.iSortOrder = value
        End Set
    End Property
    Default Public Property Item(ByVal Index As Integer) As T
        Get
            Return CType(Me.List.Item(Index), T)
        End Get
        Set(ByVal value As T)
            Me.List.Item(Index) = value
        End Set
    End Property
    Public Overridable Function GetSQL() As String
        Dim sql As String = ""
        Dim i As Integer = 0
        If Me.List.Count < 1 Then Return sql
        For i = 0 To Me.List.Count - 1
            If (sql = "") Then
                sql = "[" & Me.iTableName & "].[" & Me.List.Item(i).ToString & "]"
            Else
                sql = sql & "," & "[" & Me.iTableName & "].[" & Me.List.Item(i).ToString & "]"
            End If
        Next
        Select Case Me.iSortOrder
            Case Is = DBTOSortOrder.Ascending
                If Me.List.Count > 0 Then sql = " Order By " & sql & " ASC"
            Case Is = DBTOSortOrder.Descending
                If Me.List.Count > 0 Then sql = " Order By " & sql & " DSC"
            Case Else
                If Me.List.Count > 0 Then sql = " Order By " & sql & " ASC"
        End Select

        Return sql
    End Function

#Region "Add Overloads"
    Public Overridable Function Add(ByVal FieldName As T) As Integer
        Return Me.List.Add(FieldName.ToString)
    End Function
#End Region

#Region "Base Implementations"
    Public Overridable Function IndexOf(ByVal value As T) As Integer
        Return Me.List.IndexOf(value)
    End Function
    Public Overridable Sub Insert(ByVal index As Integer, ByVal value As T)
        Me.List.Insert(index, value)
    End Sub
    Public Overridable Overloads Sub Remove(ByVal value As T)
        Me.List.Remove(value)
    End Sub
    Public Overridable Function Contains(ByVal value As T) As Boolean
        Return Me.List.Contains(value)
    End Function
    Protected NotOverridable Overrides Sub OnValidate(ByVal value As Object)
        'If Not GetType(T).IsAssignableFrom(value.GetType()) Then
        '    Throw New ArgumentException("Value must be of type TableFilter")
        'End If
    End Sub
    Protected NotOverridable Overrides Sub OnInsert(ByVal index As Integer, ByVal value As Object)
        MyBase.OnInsert(index, value)
    End Sub
    Protected NotOverridable Overrides Sub OnRemove(ByVal index As Integer, ByVal value As Object)
        MyBase.OnRemove(index, value)
    End Sub
    Protected NotOverridable Overrides Sub OnSet(ByVal index As Integer, ByVal oldValue As Object, ByVal newValue As Object)
        MyBase.OnSet(index, oldValue, newValue)
    End Sub
#End Region

End Class